package com.aaa.dao.impl;

import com.aaa.dao.BaseDao;
import com.aaa.dao.CourseTypeDao;
import com.aaa.entity.CourseTypeEntity;
import com.aaa.entity.Pager;

import java.util.List;
import java.util.Map;

public class CourseTypeDaoImpl extends BaseDao implements CourseTypeDao {
    @Override
    public Pager queryAll(int pageNo, int pageSize) {
        String sql="select *from course_type where del=0 limit ?,?";
        Object[] params=new Object[]{(pageNo-1)*pageSize,pageSize};
        List<Map> list=this.executeQuery(sql,params);
        Integer totalCount= Integer.valueOf(queryCount().toString());
        Pager pager=new Pager(pageNo,pageSize,list,totalCount);
        return pager;
    }

    @Override
    public Map queryOne(int id) {
        String sql="select *from course_type where del=0 and id=?";
        List<Map> map=this.executeQuery(sql,id);
        return map.size()>0?map.get(0):null;
    }

    /**
     * 添加
     * @param courseType
     * @return
     */
    @Override
    public int type_add(CourseTypeEntity courseType) {
        String sql="insert into course_type (type_name) values(?)";
        return this.executeUpdate(sql,courseType.getTypeName());
    }

    /**
     * 修改
     * @param courseType
     * @return
     */
    @Override
    public int type_update(CourseTypeEntity courseType) {
        String sql="update course_type set type_name=? where id=?";
        return this.executeUpdate(sql,courseType.getTypeName(),courseType.getTypeId());
    }

    @Override
    public int type_delete(int id) {
        String sql="update course_type set del=1 where id=?";
        return this.executeUpdate(sql,id);
    }

    public Long queryCount(){
        String sql="select count(*) cnt from course_type";
        List<Map> list=this.executeQuery(sql,null);
        return (Long) list.get(0).get("cnt");
    }
}
